#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui>
#include <QMainWindow>
#include <QListView>
#include <QTreeView>
#include <QMenu>
#include <QAction>
#include <QProcess>
#include <QTableWidget>
#include <QIcon>
#include <QString>
#include <QLabel>
#include <QPushButton>
#include <QStringList>
#include <QGroupBox>
#include <QMap>
#include <QToolBar>
#include "databaseexplorermodel.h"
#include "bsonexplorermodel.h"
#include "bsonobjeditordelegate.h"
#include <client/dbclient.h>
#include <client/gridfs.h>
using namespace mongo;


class ConnectionDialog;
class BsonListModel;
class ServerOverviewWidget;
class EditCollectionWidget;
class JsEditor;
class MapReduceWidget;

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow();
    ~MainWindow();
protected:
    void closeEvent(QCloseEvent *event);
protected:
    DBClientConnection* clientConnection;
    DBClientReplicaSet* replicaSet;
    GridFS* gridFS;
protected:
    ConnectionDialog* connectionDialog;
protected:
    DatabaseExplorerModel* databaseExplorerModel;
    BsonListModel* bsonListModel;
    BsonExplorerModel* bsonExplorerModel;
    static MainWindow* pInstance;
public:
    static MainWindow* getInstance();
protected:
    //QTableView* bsonTableView;
    QListView* bsonListView;
    QTreeView* databaseExplorerView;

    JsEditor *javaScriptEditor;
    UserIDDelegate userIDDelegate;

public:

    //virtual void contextMenuEvent(QContextMenuEvent *);
    void showContextMenu(Node *pNode);
public slots:
    void SlotItemClicked(const QModelIndex& item);

protected:
    QDockWidget* databaseExplorerDockPane;
    QDockWidget* commandShowDockPane;
    QDockWidget* bsonExplorerDockPane;
    QTabWidget* centerTabViews;
    MapReduceWidget* mapReduceWidget;
signals:
    void prefsChanged();
private slots:
    //about server actions response functions
    void serverOverviewResponse();
    void addDatabaseResponse();
    void disconnectResponse();
    void closeServerResponse();
    void initReplicaResponse();
    void setReplicaResponse();
    void setShardingResponse();
    void serverStatusResponse();



    //about database actions response functions
    void addConnectionResponse();
    void responseConnection(QString connectName, DBClientConnection* clientConnect);
    void selectedDatabaseChanged();
    void showSelectedBOSNOBjTableView(BSONObj obj);


    void insertCollectionResponse();
    void databaseOverViewResponse();
    void collectionStatsResponse();
    void addUserResponse();
    void repairDatabaseResponse();
    void restoreDtatbaseResponse();
    void dropCollectionResponse();
    void runJavaScript();
    void doCollectionInsertResponse(QString collectionContent);
    void doProfilingLevel();
    void doEditJavaScriptResponse();
    bool save();
    bool saveAs();
    void about();
    void selectAll();
    void openRecentFile();
    void updateStatusBar();
    void spreadsheetModified();
    void dropIndex();
    void setPreferences();
    //for connection right button menu response

    void viewDataResponse();

    void mapReduce();

    void closeActiveWindow();
    void closeTabWindow(int index);
    void closeAllWindows();

    void renameCollectionResponse();
    void restoreCollectionResponse();

    void shardingOptionsResponse();
    void aggretationResponse();
protected:
    void createActions();
    void createMenus();
    void createToolBars();
    void createStatusBar();
    void readSettings();
    void writeSettings();
    void setCurrentFile(const QString& fileName);
    void createContextMenu();
    bool okToContinue();
    bool loadFile(const QString& fileName);
    bool saveFile(const QString& fileName);
    void updateRecentFileActions();
    QString strippedName(const QString& fullFileName);
    void printBSONObj();
public:
    QString getSelectedCollection();
    DBClientConnection* getSelectedConnection();
    QString getSelectedDb();

protected:
    static QListWidget* pTraceList;
public:
    static void AddTraceLine(QString str);

private:
    QListWidget* commandShow;
    ServerOverviewWidget* serverOverview;
    EditCollectionWidget* editCollection;
    QLabel* locationLabel;
    QLabel* formulaLabel;
    QStringList recentFiles;
    QString curFile;
    enum{MaxRecentFiles=5};

    QMenu* fileMenu;
    QMenu* serverMenu;
    QMenu* databaseMenu;
    QMenu* collectionMenu;
    QMenu* indexMenu;
    QMenu* javascriptMenu;
    QMenu* toolsMenu;
    QMenu* windowMenu;
    QMenu* helpMenu;

    QToolBar* fileToolBar;
    QToolBar* databaseToolBar;
    QToolBar* collectionToolBar;

    QAction* addConnectionAction;
    QAction* exitAction;



    QAction* serverOverviewAction;
    QAction* addDatabaseAction;
    QAction* disconnectAction;
    QAction* serverStatusAction;
    QAction* closeServerAction;
    QAction* initReplicaAction;
    QAction* setReplicaAction;
    QAction* setShardingAction;

    QAction* addUserAction;
    QAction* databaseOverViewAction;
    QAction* profilingLevelAction;
    QAction* addDatabaseConnectionAction;
    QAction* addJavascriptAction;
    QAction* importAction;
    QAction* copyCollections2Other;
    QAction* dropDatabaseAction;
    QAction* statsDatabaseAction;
    QAction* repairDatabaseAction;
    QAction* initGFSAction;
    QAction* restoreAction;
    QAction* runJSAction;
    QAction* backupDBAction;
    QAction* sqlConvertAction;


    QAction* viewAction;
    QAction* findAction;
    QAction* find2Action;
    QAction* updateAction;
    QAction* mapReduceAction;
    QAction* insertCollectionAction;
    QAction* addIndexAction;
    QAction* duplicateCollection;
    QAction* copyCollection2Other;
    QAction* removeAction;
    QAction* removeAllAction;
    QAction* dropCollectionAction;
    QAction* statsCollectionAction;
    QAction* renameCollectionAction;
    QAction* backupCollectionAction;
    QAction* importCollectionAction;
    QAction* exportCollectionAction;
    QAction* compactCollectionAction;
    QAction* indexManageAction;
    QAction* reIndexAction;
    QAction* restoreCollectionAction;

    QAction* dropIndexAction;
    QAction* propertiesAction;
    QAction* editJavascriptAction;

    QAction* serverMonitoringAction;
    QAction* preferencesAction;
    QAction* shardingOptionsAction;
    QAction* aggretationAction;

    QAction* closeAction;
    QAction* closeAllAction;

    QAction* licenseManagerAction;
    QAction* aboutAction;
};

#endif // MAINWINDOW_H
